/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.pos.inventory;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.inventory.model.ProductPrefPriceTableModel;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.bean.ProductPrefPrice;
import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.epos.panel.AbstractJModelFinishingPanel;
import java.awt.event.ActionEvent;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 *
 * @author Administrator
 */
public class JProductPrefPricePanel  extends AbstractJModelFinishingPanel<ProductPrefPrice,ProductPrefPrice>{
	public JProductPrefPricePanel() {
		super(new ProductPrefPriceTableModel());
		
	}

    @Override
    public void activate() throws BasicException {
    }

    @Override
    public String getTitle() {
        return AppLocal.getIntString("Menu.PrefPrice");
    }


	@Override
	public boolean deactivate() {
		return true;
	}

	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public void initFinishPanel(AppView app) throws BeanFactoryException {
		timeCycleType = QuickTimerFilter.TIMERCYCLETYPE_Null;
	}

	@Override
	public Object createValue() throws BasicException {
		Object[] afilter=new Object[6];
		
		// Date range
		Object startdate = Formats.TIMESTAMP
				.parseValue(jTxtStartDate.getText());
		Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

		afilter[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS;
		afilter[1] = startdate;
		afilter[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS;
		afilter[3] = enddate;

		//product
		afilter[4] = QBFCompareEnum.COMP_NONE;
		afilter[5] = null;
		
		return afilter;
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if (beforeFilterParas == null)
			try {
				beforeFilterParas = (Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
		}
		
		// Date range
		Object startdate = e.getStartDate();
		Object enddate = e.getEndDate();

		beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS;
		beforeFilterParas[1] = startdate;
		beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS;
		beforeFilterParas[3] = enddate;
		reQuery();
		
	}

	@Override
	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
		String sCode = inputCode.getText();
		if (sCode.isEmpty()) {
			return;
		}
		if (beforeFilterParas == null)
			try {
				beforeFilterParas = (Object[]) createValue();
			} catch (BasicException e1) {
				new MessageInf(e1).show(this);
				return;
			}
	}

	@Override
	protected void configNavigator(JPanel jNavigatorToolBar, JPanel jNavigator) {
	}

	@Override
	public void reQuery() {
		try {
			tableModel.resert(dlSales.listByFilter(ProductPrefPrice.class, beforeFilterParas));
			if (beforeFilterParas[1] != null)
				jTxtStartDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[1]));
			else
				jTxtStartDate.setText(null);
			if (beforeFilterParas[3] != null)
				jTxtEndDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[3]));
			else
				jTxtEndDate.setText(null);
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
		}
		
	}

	@Override
	protected void configToolBar(JToolBar jToolBar12) {
	}

	@Override
	protected AbstractDAOJEditor<ProductPrefPrice> getEditor() {
		return new JProductPrefPriceEditor(mode.getDLSales(), new DirtyManager(), (AppView) app);
	}

	@Override
	protected ProductPrefPrice convectToViewModel(ProductPrefPrice obj) {
	   return obj;
	}

	@Override
	protected ProductPrefPrice convectToEditModel(ProductPrefPrice obj)
			throws BasicException {
		return obj;
	}

	@Override
	public String getModelName() {
		return ERP_UI_Swing_Model.MODEL_NAME;
	}

	
}
